package com.zh.business.base.exception;

import java.io.PrintStream;
import java.io.PrintWriter;

/**
 * 异常抛出类
 * @Description:异常抛出类   
 * @author: Yang 
 * @date:   2017年9月30日
 */
public class MsgException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    private String _msg;

    private int code;

    public static final MsgException exception() {
        return new MsgException();
    }

    public static final MsgException exception(int code) {
        return new MsgException(code);
    }

    public static final MsgException exception(String msg) {
        return new MsgException(msg);
    }

    public static final MsgException exception(String msg, int code) {
        return new MsgException(msg, code);
    }

    public MsgException() {
        _msg = "系统错误";
        code = 500;
    }

    /**
     * @param msg 用于显示的消息
     */
    public MsgException(String msg) {
        this();
        _msg = msg;
        code = -1;
    }

    /**
     * @param code 错误码
     */
    public MsgException(int code) {
        this();
        this.code = code;
    }

    /**
     * @param msg  用于显示的消息
     * @param code 错误码
     */
    public MsgException(String msg, int code) {
        _msg = msg;
        this.code = code;
    }

    @Override
    public void printStackTrace() {
    }

    @Override
    public void printStackTrace(PrintStream s) {
        //s.print(_msg);
    }

    @Override
    public void printStackTrace(PrintWriter s) {
        //s.print(_msg);
    }

    @Override
    public StackTraceElement[] getStackTrace() {
        return null;
    }

    @Override
    public String getMessage() {
        return _msg;
    }

    @Override
    public String getLocalizedMessage() {
        return _msg;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    @Override
    public String toString() {
        return "{code: " + code + ", " +
                "message: " + _msg + "}";
    }
}
